home *** CD-ROM | disk | FTP | other *** search
- /*
- File: ThreadUtil.h
-
- Contains: External Interface to Thread Manager Utilities
-
- Written by: Brad Post, Eric Anderson and Bill Knott
-
- Copyright: © 1993 by Apple Computer, Inc., all rights reserved.
-
- Change History (most recent first):
-
- 4/19/93 bsp Added the Boolean busy to the LinkedList Semaphore model. This is because
- we were getting interrupted while releasing the semaphore and appending some
- one to the list of people waiting....
- 3/3/93 bsp Added a lastInLine element for the LinkedListSemaphore model.
- 2/16/93 bsp Added the prototype for InitializeThreadUtilities.
- 2/9/93 bsp Cleaned up the interface.
- 2/8/93 bsp Changed waitList to a **, made all functions PASCAL. Changed
- prototypes to use anArraySemaphorePtr as well as pascal.
- 2/5/93 bsp Added prototypes for CreateLinkedListSemaphore, DeleteLinkedListSemaphore,
- DeleteSimpleSemaphore and CreateSimpleSemaphore. Added the *aLinkedListSemaphorePtr,
- *aSimpleSemaphorePtr, and *anArraySemaphorePtr.
- 2/4/93 bsp reversed the meaning of isAvailable and notAvailabe to work
- with our TestAndSetByte routine
- 2/2/93 bsp Added prototypes and definitions for handling semaphores
- 1/31/93 ewa New Today.
-
- */
-
- #ifndef __THREADUTIL__
- #define __THREADUTIL__
-
- #include <Timer.h>
- #include <Threads.h>
-
- /* **********************************************************
- * Defines used by this library
- * **********************************************************
- */
-
- /* **********************************************************
- * Here are some of the error codes that can be returned.
- * **********************************************************
- */
-
- #define semaphoreButtHeadErr 1 // If there is something stupid going on
- #define semaphoreWaitErr 2 // Returned if you try to delete a semaphore and there are threads still waiting for it
-
-
- /* **********************************************************
- * Structs & typedefs used by this library
- * **********************************************************
- */
-
- /* **********************************************************
- * These typedefs are used only by the SimpleSemaphore model
- * **********************************************************
- */
-
- typedef Boolean SimpleSemaphore;
- typedef SimpleSemaphore *SimpleSemaphorePtr;
-
- /* ***************************************************************************
- * These typedefs and structs are used only by the LinkedList Semaphore model
- * ***************************************************************************
- */
-
- struct LinkedListElement {
- ThreadID whoAmI;
- struct LinkedListElement *next; // Next thread waiting for semaphore
- };
-
- typedef struct LinkedListElement LinkedListElement;
- typedef LinkedListElement *LinkedListElementPtr;
-
- struct LinkedListSemaphore
- {
- Boolean available; // Is the semaphore available
- Boolean busy; // Are we busy trying to get the semaphore or releasing it???
- ThreadID theHolder; // Who is the current holder
- LinkedListElementPtr waitingList; // List of Threads waiting for Semaphore
- LinkedListElementPtr lastInList; // Last thread waiting
- };
-
- typedef struct LinkedListSemaphore LinkedListSemaphore;
- typedef LinkedListSemaphore *LinkedListSemaphorePtr;
-
- /* **************************************************************************
- * These typedefs and structs are only used by the Array Semaphore model
- * **************************************************************************
- */
-
- struct ArraySemaphore
- {
- Boolean available; // is the semaphore available
- ThreadID theHolder; // who is the current holder
- long whoseNext; // whose next to be run
- long nextSpot; // next spot in the List to place a person
- long numberWaiting; // number of Threads waiting
- long maxInQueue; // maximum number of Threads in the queue
- ThreadID **waitList; // ID's of the number waiting
- };
-
- typedef struct ArraySemaphore ArraySemaphore;
- typedef ArraySemaphore *ArraySemaphorePtr;
-
-
- /* **********************************************************
- * Routine prototypes
- * **********************************************************
- */
-
- pascal void InitializeThreadUtilities();
-
- pascal void DelayThread (unsigned long millisecDelay);
-
- /* **********************************************************
- * These are the rountines for using Simple Semaphores
- * **********************************************************
- */
-
- pascal OSErr CreateSimpleSemaphore( SimpleSemaphorePtr *aSemaphore );
- pascal OSErr GetSimpleSemaphore( SimpleSemaphorePtr aSemaphore );
- pascal OSErr ReleaseSimpleSemaphore( SimpleSemaphorePtr aSemaphore );
- pascal OSErr DeleteSimpleSemaphore( SimpleSemaphorePtr aSemaphore );
-
- /* **********************************************************
- * These are the rountines for using LinkedList Semaphores
- * **********************************************************
- */
-
- pascal OSErr CreateLinkedListSemaphore( LinkedListSemaphorePtr *aSemaphore );
- pascal OSErr GetLinkedListSemaphore( LinkedListElement *whichOne, LinkedListSemaphorePtr aSemaphore );
- pascal OSErr ReleaseLinkedListSemaphore( LinkedListSemaphorePtr aSemaphore );
- pascal OSErr DeleteLinkedListSemaphore( LinkedListSemaphorePtr aSemaphore);
-
- /* **********************************************************
- * These are the rountines for using Array Semaphores
- * **********************************************************
- */
-
- pascal OSErr CreateArraySemaphore(long maxInQueue, ArraySemaphorePtr *aSemaphore);
- pascal OSErr GetArraySemaphore( ArraySemaphorePtr aSemaphore );
- pascal OSErr ReleaseArraySemaphore( ArraySemaphorePtr aSemaphore);
- pascal OSErr DeleteArraySemaphore( ArraySemaphorePtr aSemaphore );
-
- #endif /* __THREADUTIL__ */
-